package cn.herlioner.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.herlioner.dao.impl.CollegeDAOImpl;
import cn.herlioner.dao.impl.Obj_TeaDAOImpl;
import cn.herlioner.dao.impl.ObjectDAOImpl;
import cn.herlioner.dao.impl.TeacherDAOImpl;
import cn.herlioner.pojo.College;
import cn.herlioner.pojo.Obj_Tea;
import cn.herlioner.pojo.Teacher;
import cn.herlioner.pojo.TSObject;
import cn.herlioner.pojo.CRUObject;
import cn.herlioner.service.CRUObjectService;
import cn.herlioner.service.PageService;
import cn.herlioner.test.Clg;

public class Obj_TeaAction extends ActionSupport implements SessionAware{
	Obj_Tea obj_Tea;
	Obj_TeaDAOImpl obj_TeaDAO  = new Obj_TeaDAOImpl();
	List<Obj_Tea> obj_TeaDAOs;
	
	Teacher teacher;
	TeacherDAOImpl teacherDAO = new TeacherDAOImpl();
	
	TSObject tSObject;
	ObjectDAOImpl objectDAO = new ObjectDAOImpl();
	
	College college;
	CollegeDAOImpl collegeDAO = new CollegeDAOImpl();
	
	CRUObject cruObject; 
	List<CRUObject> cruObjects = new ArrayList();
	CRUObjectService cruObjectService = new CRUObjectService();
	
	private int currentPagechange;
	
	public String ObjToTeacher(){
		//获取组合表里面的组合信息
		obj_TeaDAOs = obj_TeaDAO.list();
		//根据两组num来制作�?�?��端的信息列表
		for (Obj_Tea OT : obj_TeaDAOs) {
			//得到对应的课程和教师代码
			Long objnum = OT.getObjnum();
			Long teanum = OT.getTeanum();
			//根据课程和教师代码获取对应的教师和课程信�?
			teacher =  teacherDAO.get(teanum);
			tSObject = objectDAO.get(objnum).get(0);
			if(teacher!=null&&tSObject!=null){
//			cruObject = cruObjectService.setobject(object, cruObject);
			}else{
				//可以用作日志输出
				System.out.println("有�?为空");
			}
			cruObjects.add(cruObject);
		}
		ActionContext.getContext().getSession().put("cruObjects", cruObjects);
		return "success";
	}
	
	public String tea_objmanager(){
		Map session = ActionContext.getContext().getSession();
		obj_TeaDAOs = obj_TeaDAO.listbyteanum(teacher.getTeanum());
		if(session.get("cruObjects") != null){
			session.remove("cruObjects");
		}
		for (Obj_Tea OT : obj_TeaDAOs) {
			//得到对应的课程和教师代码
			Long objnum = OT.getObjnum();
			Long teanum = OT.getTeanum();
			//根据课程和教师代码获取对应的教师和课程信�?
			teacher =  teacherDAO.get(teanum);
			tSObject = objectDAO.get(objnum).get(0);
			CRUObject cruObject = new CRUObject();
			//只需显示教师名字，教师院属�?，课程名字，以及对应的操作，
			cruObject.setTeaname(teacher.getTeaname());
			cruObject.setObjname(tSObject.getObjname());
			cruObject.setObjnum(tSObject.getObjnum());
			cruObject.setClgname(collegeDAO.get(teacher.getClgnum()).getClgname());
			cruObjects.add(cruObject);
		}
		session.put("cruObjects", cruObjects);
		return "success";
	}
	
	public String teacherobject(){
		Map session = ActionContext.getContext().getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
		Long teanum = (Long) session.get("Num");
		int pageSize = 7 ;
		
		if(request.getParameter("currentPagechange")==null){
			currentPagechange = (int)session.get("currentPagechange");
			System.out.println(currentPagechange);
		}else{
			currentPagechange = Integer.valueOf(request.getParameter("currentPagechange"));
		}
		
		PageService Pageservice = obj_TeaDAO.listbyteanum(currentPagechange, pageSize,teanum);
		Pageservice.toString();
		try{
			for (int i = 0 ;i<pageSize;i++) {
		    	CRUObject cruObject = new CRUObject();
		    	obj_Tea = (Obj_Tea)Pageservice.getRecordList().get(i);
		    	
		    	cruObject = cruObjectService.setobject(obj_Tea,cruObject);
		    	
		    	cruObjects.add(cruObject);}
			}catch (IndexOutOfBoundsException e) {
				// TODO: handle exception
				System.out.println("尾页不够pageSize了，但是不影响输出?");
			}
			//如果已经存在信息，则应该删除，然后更新信�?
		request.setAttribute("cruObjects", cruObjects);
    	request.setAttribute("PageService", Pageservice);
		
		
		return "teacherobjectSuccess";
	}
	
	@Override
	public void setSession(Map<String, java.lang.Object> arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public College getCollege() {
		return college;
	}
	public void setCollege(College college) {
		this.college = college;
	}
	public CollegeDAOImpl getCollegeDAO() {
		return collegeDAO;
	}
	public void setCollegeDAO(CollegeDAOImpl collegeDAO) {
		this.collegeDAO = collegeDAO;
	}
	public CRUObjectService getCruObjectService() {
		return cruObjectService;
	}
	public void setCruObjectService(CRUObjectService cruObjectService) {
		this.cruObjectService = cruObjectService;
	}
	public void setCruObjects(List<CRUObject> cruObjects) {
		this.cruObjects = cruObjects;
	}
	public CRUObject getCruObject() {
		return cruObject;
	}
	public void setCruObject(CRUObject cruObject) {
		this.cruObject = cruObject;
	}
	public List<CRUObject> getCruObjects() {
		return cruObjects;
	}
	public void setCruObjects(ArrayList<CRUObject> cruObjects) {
		this.cruObjects = cruObjects;
	}
	public void setObj_TeaDAOs(List<Obj_Tea> obj_TeaDAOs) {
		this.obj_TeaDAOs = obj_TeaDAOs;
	}
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	public TeacherDAOImpl getTeacherDAO() {
		return teacherDAO;
	}
	public void setTeacherDAO(TeacherDAOImpl teacherDAO) {
		this.teacherDAO = teacherDAO;
	}
	public TSObject getObject() {
		return tSObject;
	}
	public void setObject(TSObject tSObject) {
		this.tSObject = tSObject;
	}
	public ObjectDAOImpl getObjectDAO() {
		return objectDAO;
	}
	public void setObjectDAO(ObjectDAOImpl objectDAO) {
		this.objectDAO = objectDAO;
	}
	public Obj_Tea getObj_Tea() {
		return obj_Tea;
	}
	public void setObj_Tea(Obj_Tea obj_Tea) {
		this.obj_Tea = obj_Tea;
	}
	public Obj_TeaDAOImpl getObj_TeaDAO() {
		return obj_TeaDAO;
	}
	public void setObj_TeaDAO(Obj_TeaDAOImpl obj_TeaDAO) {
		this.obj_TeaDAO = obj_TeaDAO;
	}
	public List<Obj_Tea> getObj_TeaDAOs() {
		return obj_TeaDAOs;
	}
	public void setObj_TeaDAOscr(List<Obj_Tea> obj_TeaDAOs) {
		this.obj_TeaDAOs = obj_TeaDAOs;
	}
}
